//
//  MasterDetailViewController.swift
//  健身宝
//
//  Created by hzt on 2017/10/10.
//  Copyright © 2017年 hzt. All rights reserved.
//

import UIKit
class MasterDetailViewController: UIViewController {
    var infoDict:NSDictionary!
    var dict:NSDictionary!
    var videoPlayer:VideoPlayer!
    var hideState = false
    var askList:NSMutableArray!
    override func viewDidLoad() {
        super.viewDidLoad()
        viewConfig()
        getAskList()
    }
    
    func viewConfig(){
        view.backgroundColor = UIColor.white
        
//        let navBar = CustomNavBar(frame: CGRect(origin: .zero, size: CGSize(width: WIDTH, height: 64)))
//        navBar.initialize(text: nil)
//        view.addSubview(navBar)
        self.navigationController?.isNavigationBarHidden = true
        self.rdv_tabBarController?.isTabBarHidden = true
        let imageView = UIImageView()
        let pURL = "http://172.24.10.175/workout/Uploads/"
        imageView.sd_setImage(with:URL(string: pURL+(infoDict.value(forKey: "thumb") as! String)), placeholderImage: UIImage(named:"placeholder"))
        imageView.frame = CGRect(x: 0, y: 64, width: UIScreen.main.bounds.width, height: (UIScreen.main.bounds.height-69)/3)
        view.addSubview(imageView)
        
        let label = UILabel(frame: CGRect(x: 15, y: imageView.bounds.height+74, width: UIScreen.main.bounds.width-30, height: 40))
        label.text = "视频简介"
        label.textAlignment = .center
        label.layer.cornerRadius = 5
        label.layer.borderColor = UIColor.lightGray.cgColor
        //label.layer.borderWidth = 1
        view.addSubview(label)
        
        let Insert_time = UILabel(frame: CGRect(x: 15, y: label.frame.origin.y+260, width: UIScreen.main.bounds.width-30, height: 20))
        Insert_time.text = "发布时间:\(infoDict.value(forKey: "insert_time") as! String)"
        Insert_time.font = UIFont.systemFont(ofSize: 20)
        view.addSubview(Insert_time)
        
        let addrLabel = UILabel(frame: CGRect(x: 15, y: Insert_time.frame.origin.y+40, width: UIScreen.main.bounds.width-30, height: 20))
        let addr = (infoDict.value(forKey: "province_name") as! String)+(infoDict.value(forKey: "city_name")  as! String)
        addrLabel.text = "具体地址: \(addr)"
        addrLabel.font = UIFont.systemFont(ofSize: 20)
        view.addSubview(addrLabel)
        
        let Hit = UILabel(frame: CGRect(x: 15, y: addrLabel.frame.origin.y+40, width: UIScreen.main.bounds.width-30, height: 20))
        Hit.text = "点击次数:\(infoDict.value(forKey: "hits") as! String)"
        Hit.font = UIFont.systemFont(ofSize: 20)
        view.addSubview(Hit)
        
        let joinBtn = UIButton(frame: CGRect(x: 0, y: Hit.frame.origin.y+40, width: 130, height: 44))
        joinBtn.backgroundColor = UIColor.red
        joinBtn.center.x = UIScreen.main.bounds.width/3
        joinBtn.setTitle("关注", for: .normal)
        joinBtn.layer.cornerRadius = 8
        joinBtn.adjustsImageWhenDisabled = false
        view.addSubview(joinBtn)
        joinBtn.addTarget(self, action: #selector(add), for: .touchUpInside)
        
        let AskBtn = UIButton(frame: CGRect(x: 0, y: Hit.frame.origin.y+40, width: 130, height: 44))
        AskBtn.backgroundColor = UIColor.red
        AskBtn.center.x = UIScreen.main.bounds.width/3*2
        AskBtn.setTitle("提问", for: .normal)
        AskBtn.layer.cornerRadius = 8
        AskBtn.adjustsImageWhenDisabled = false
        view.addSubview(AskBtn)
        AskBtn.addTarget(self, action: #selector(ask), for: .touchUpInside)
        
        videoPlayer = VideoPlayer(frame: CGRect(x: 0, y: label.frame.origin.y+50, width: UIScreen.main.bounds.width, height: 200))
        videoPlayer.initialize(itemUrl:RO_Video)
        view.addSubview(videoPlayer)
        
        let backBtn = UIButton(frame: CGRect(x: 16, y: 15, width: 35, height: 30))
        backBtn.titleLabel?.font = UIFont.systemFont(ofSize: 17)
        backBtn.setTitle("返回", for: .normal)
        backBtn.setTitleColor(.black, for: .normal)
        view.addSubview(backBtn)
        backBtn.addTarget(self, action: #selector(back), for: .touchUpInside)

//        
//        navBar.backBtn.addTarget(self, action: #selector(back), for: .touchUpInside)
        
        videoPlayer.fullBtn.addTarget(self, action: #selector(fullScreen), for: .touchUpInside)
    }
    
    @objc func add(sender:UIButton){
        sender.isEnabled = false
        let alert = UIAlertController(title: "是否关注", message: nil, preferredStyle: .alert)
        let confirmAction = UIAlertAction(title: "确认", style: .default) { (_) in
            let parameter:NSDictionary = [
                "SessionID"     :   LoginModel.sessionID,
                "mvideo_id"     :   self.infoDict.value(forKey: "id") as! String,
                "user_id"       :   LoginModel.userID
            ]
            print(parameter)
            RONetworkMngTool.shared.focusMaster(parameter: parameter, block: {
                DispatchQueue.main.async {
                    let popup = Popup()
                    popup.appear(title: "关注成功", action: {
                        self.back()
                    })
                }
            })
        }
        let cancelAction = UIAlertAction(title: "取消", style: .default, handler: nil)
        alert.addAction(confirmAction)
        alert.addAction(cancelAction)
        present(alert, animated: true, completion: nil)
    }
    
    func getAskList(){
        let urlStr = RO_AskList
        let paraDict = ["sessionid":SessionID]
        AFHTTPRequestOperationManager().post(urlStr, parameters: paraDict, success: { (response:AFHTTPRequestOperation?, data:Any?) in
            let array = data as! NSArray
            //print(array)
            self.askList = array.mutableCopy() as! NSMutableArray
        }) { (response:AFHTTPRequestOperation?, error:Error?) in
            print(response!.debugDescription)
        }

    }
    
    @objc func ask(sender:UIButton){
                sender.isEnabled = false
                //let dict = askList![indexPath.row] as! NSDictionary
                //print(dict)
                let commentArray = self.askList as! NSArray
                let controller = AskDetailController()
                controller.infoArray = commentArray.mutableCopy() as! NSMutableArray
                controller.userID = dict.value(forKey: "user_id") as! String
                controller.askID = dict.value(forKey: "id") as! String
                controller.masterID = dict.value(forKey: "master_id") as! String
                //print(controller.userID)
        
                //hidesBottomBarWhenPushed = true
                _ = navigationController?.pushViewController(controller, animated: true)
                //hidesBottomBarWhenPushed = false

//        let controller = AskDetailController()
//        controller.masterID = infoDict.value(forKey: "id") as! String
//        //let commentArray =
////        hidesBottomBarWhenPushed = true
//        _ = navigationController?.pushViewController(controller, animated: true)
    }

//    @objc func ask(sender:UIButton){
//        sender.isEnabled = false
//        let dict = askList![indexPath.row] as! NSDictionary
//        //print(dict)
//        let commentArray = dict.value(forKey: "question") as! NSArray
//        let controller = AskDetailController()
//        controller.infoArray = commentArray.mutableCopy() as! NSMutableArray
//        controller.userID = dict.value(forKey: "userid") as! String
//        controller.askID = dict.value(forKey: "id") as! String
//        //print(controller.userID)
//        
//        //hidesBottomBarWhenPushed = true
//        _ = navigationController?.pushViewController(controller, animated: true)
//        //hidesBottomBarWhenPushed = false
//    }
    
    @objc func fullScreen(){
        let pw = videoPlayer.bounds.width
        let ph = videoPlayer.bounds.height
        if !hideState{
            UIView.animate(withDuration: 0.3, animations: {
                self.videoPlayer.transform = CGAffineTransform(rotationAngle: CGFloat(Double.pi/180)*CGFloat(90)).concatenating(CGAffineTransform(scaleX: UIScreen.main.bounds.width/ph, y: UIScreen.main.bounds.height/pw)).concatenating(CGAffineTransform(translationX: 0, y: -25.833-64))
            })
            hideState = !hideState
            setNeedsStatusBarAppearanceUpdate()
        }
        else{
            UIView.animate(withDuration: 0.3, animations: {
                self.videoPlayer.transform = .identity
            })
            hideState = !hideState
            setNeedsStatusBarAppearanceUpdate()
        }
    }
    
    override var prefersStatusBarHidden: Bool{
        return hideState
    }
    
    @objc func back(){
        videoPlayer.disAppear()
        _ = navigationController?.popViewController(animated: true)
        self.navigationController?.isNavigationBarHidden = false
        self.rdv_tabBarController?.isTabBarHidden = false
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
}
